package com.lee.demo1.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.codehaus.jettison.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lee.demo1.business.InstructonsBusiness;
import com.lee.demo1.domain.Instructons;
import com.lee.demo1.domain.InstructonsCategory;
import com.lee.demo1.domain.Tag;
import com.lee.demo1.util.pager.Pager;
import com.lee.demo1.util.pager.PagerHelper;


/**
 * 药品说明书页面控制处理跳转类
 * @author leopad
 *
 */
@Controller
@RequestMapping("instructons")
public class InstructonsController {

	private static Logger logger = LoggerFactory.getLogger(InstructonsController.class);
	
	@Resource
	private InstructonsBusiness instructonsBusiness;
	
	/**
	 * 验证药品名称是否重复
	 * @return
	 */
	@RequestMapping(params = "method=validateDrugNames")
	@ResponseBody
	public String validateDrugNames(String fieldId, String fieldValue){
		JSONArray json = new JSONArray();
		
		//logger.debug("-----------------" + fieldId);
		//logger.debug("-----------------" + fieldValue);
		
		//boolean flag = userService.checkUserName(username);
		boolean flag = true;
		
		json.put(fieldId);
		json.put(flag);
		
		return json.toString();
	}
	
	
	/**
	 * 药品说明书列表页面（包括分页、查询）
	 * @param model
	 * @param ins
	 * @param request
	 * @return
	 */
	@RequestMapping
	public String load(Model model, Instructons ins, HttpServletRequest request){
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("drugNames", (String)ins.getDrugNames());
		
		int totalRows = instructonsBusiness.getInstructonsCount(map);
		Pager pager = PagerHelper.getPager(request,totalRows);
		map.put("begin", pager.getStartRow());
		map.put("size", pager.getPageSize());
		
		List<Instructons> instructonsList = instructonsBusiness.getInstructonsList(map);
		
		model.addAttribute("instructonsList", instructonsList);
		model.addAttribute("drugNames",ins.getDrugNames());
		model.addAttribute("pager",pager);
		return "admin/instructons/list";
	}
	
	/**
	 * 说明书录入页面
	 * @return
	 */
	@RequestMapping(params = "method=create")
	public String create(Model model){
		
		//1、取出类别
		List<InstructonsCategory> ic = instructonsBusiness.getInstructonsCategories();
		
		//2、设置tag
		List<Tag> tags = instructonsBusiness.getTags();
		
		model.addAttribute("categories" ,ic);
		model.addAttribute("tags" ,tags);
		
		return "admin/instructons/form";
	}
	
	/**
	 * 说明书录入页面
	 * @return
	 */
	@RequestMapping(params = "method=update")
	public String update(Model model,int id){
		logger.debug("-------------------" + id);
		
		model.addAttribute("ins",instructonsBusiness.getInstructons(id));
		
		//1、取出类别
		List<InstructonsCategory> ic = instructonsBusiness.getInstructonsCategories();
		
		//2、设置tag
		List<Tag> tags = instructonsBusiness.getTags();
		
		model.addAttribute("categories" ,ic);
		model.addAttribute("tags" ,tags);
		
		return "admin/instructons/form";
	}
	/**
	 * 说明书保存方法
	 * @param instructons
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(params = "method=save", method = RequestMethod.POST)
	public String save(Instructons instructons, 
			RedirectAttributes redirectAttributes, 
			HttpServletRequest request) throws Exception{
		
		int id = instructonsBusiness.saveInstructons(instructons,request);
		
		//logger.debug("--------------" + id);
		redirectAttributes.addFlashAttribute("message", instructons.getDrugNames() );
		return "redirect:/instructons?method=create";
	}
	
	/**
	 * 页面上字符串日期传入controller时会报错，通过这个方法进行转换成Date类型
	 * @param binder
	 */
	@InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
